package com.yzh.wanandroid.repository.user

import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.MutableLiveData
import com.yzh.core.livedata.SingleLiveEvent
import com.yzh.wanandroid.base.IBaseUserRepository
import com.yzh.wanandroid.model.RegistInfoData
import com.yzh.wanandroid.model.StateData
import com.yzh.wanandroid.model.UserData
import com.yzh.wanandroid.network.retrofit.RetrofitFactory
import com.yzh.wanandroid.network.retrofit.api.UserServer
import com.yzh.wanandroid.network.retrofit.responsebean.BaseData
import com.yzh.wanandroid.network.retrofit.responsebean.LoginData
import com.yzh.wanandroid.network.retrofit.responsebean.RegisterData
import com.yzh.wanandroid.network.retrofit.responsebean.UserInfo
import com.yzh.wanandroid.repository.BaseRepository
import com.yzh.wanandroid.utils.*
import org.greenrobot.eventbus.EventBus

class MainRepository : IBaseUserRepository {

    override fun loadUserInfo(
        owner: LifecycleOwner,
        userInfoData: MutableLiveData<StateData<BaseData<UserInfo>>>
    ) {
        BaseRepository<BaseData<UserInfo>>(owner) {
            loadData {
                RetrofitFactory.createApi(UserServer::class.java).getUserInfo()
            }.success {
                userInfoData.value = StateData(data = it)
            }.error { code, message ->
                userInfoData.value = StateData(messageCode = code, message = message)
            }
        }
    }

    override fun login(
        owner: LifecycleOwner,
        username: String,
        password: String,
        loginData: SingleLiveEvent<StateData<BaseData<LoginData>>>
    ) {
        BaseRepository<BaseData<LoginData>>(owner) {
            loadData {
                RetrofitFactory.createApi(UserServer::class.java).login(username, password)
            }.loading {
                loginData.value = StateData(loadingState = LOADING)
            }.success {
                if (it.errorCode != 0) {
                    loginData.value = StateData(
                        loadingState = ERROR,
                        messageCode = it.errorCode,
                        message = it.errorMsg
                    )
                } else {
                    loginData.value = StateData(loadingState = SUCCESS, data = it)
                    EventBus.getDefault().post(UserData(it.data.username))
                    SpUtils.putBoolean(IS_LOGIN, true)
                }
            }.error { code, message ->
                loginData.value =
                    StateData(loadingState = ERROR, messageCode = code, message = message)
            }
        }
    }

    override fun logOut(owner: LifecycleOwner, logOutState: MutableLiveData<Int>) {
        BaseRepository<BaseData<*>>(owner) {
            loadData {
                RetrofitFactory.createApi(UserServer::class.java).logout()
            }.loading {
                logOutState.value = LOGOUT_LOADING
            }.success {
                logOutState.value = LOGOUT_SUCCESS
            }.error { _, _ ->
                logOutState.value = LOGOUT_ERROR
            }
        }
    }

    override fun register(
        owner: LifecycleOwner,
        username: String,
        password: String,
        repassword: String,
        register: SingleLiveEvent<StateData<RegistInfoData>>
    ) {
        BaseRepository<BaseData<RegisterData>>(owner) {
            loadData {
                RetrofitFactory.createApi(UserServer::class.java)
                    .register(username, password, repassword)
            }.loading {
                register.value = StateData(loadingState = LOADING)
            }.success {
                val registInfoData = RegistInfoData(username, password)
                if (it.errorCode < 0) {
                    register.value = StateData(loadingState = ERROR, message = it.errorMsg)
                } else {
                    register.value = StateData(loadingState = SUCCESS, data = registInfoData)
                }
            }.error { code, message ->
                register.value =
                    StateData(loadingState = ERROR, messageCode = code, message = message)
            }
        }
    }
}